#include "build.h"
#include "fig_ui_levelselect.h"
#include "fig_vars.h"
#include "fig_constants.h"
//#include "fig_ui_removead.h"
#include "gen_game_state.h"

#ifndef PRO_VERSION
#include "admanager/admanager.h"
#endif

#include "ui/ui_checkbox.h"
#include "ui/ui_manager.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "gameflow/ui_state.h"
#include "font/font_manager.h"
#include "graphics/graphics.h"
#include "graphics/graphics_util.h"
#include "application/game_application.h"


#include "util/log.h"
#include "util/profiler.h"




using namespace gameflow;



FIGUILevelSelect::FIGUILevelSelect(camera::FbCamera*	pCamera) :
				FIGMOGAUIState(pCamera, "ui/level.ui")
	{
        
#ifndef _PLATFORM_IOS

	char Buffer[256];
	for(int i = 0; i < FIGConstants::LEVEL_LAST; i++)
	{
		sprintf(Buffer, "level_button%i" , i+1);
		AddToNaviationUI( GetWidget(Buffer));
	}
#endif

   
	m_pUIEasy = m_Panels[0]->GetCheckBoxByName("easy_button");
	m_pUINormal= m_Panels[0]->GetCheckBoxByName("normal_button");
	m_pUIHard = m_Panels[0]->GetCheckBoxByName("hard_button");
	m_pUIBuyFullVersion = GetWidget("BUY_FULLVERSION"); 
	m_pUIPlayButton = GetWidget("start_button");

	m_FirstButtonPos = m_pUIBuyFullVersion->GetPosition();
	m_SecondButtonPos= m_pUIPlayButton->GetPosition();

	SetWidgetFunctor("easy_button",UI_DELEGATE(FIGUILevelSelect, OnButtonEasy));
	SetWidgetFunctor("normal_button",UI_DELEGATE(FIGUILevelSelect, OnButtonNormal));
	SetWidgetFunctor("hard_button",UI_DELEGATE(FIGUILevelSelect, OnButtonHard));

	SetWidgetFunctor("level_button1",UI_DELEGATE(FIGUILevelSelect, OnButtonLevel_1));
	SetWidgetFunctor("level_button2",UI_DELEGATE(FIGUILevelSelect, OnButtonLevel_2));
	SetWidgetFunctor("level_button3",UI_DELEGATE(FIGUILevelSelect, OnButtonLevel_3));
	SetWidgetFunctor("level_button4",UI_DELEGATE(FIGUILevelSelect, OnButtonLevel_4));

	SetWidgetFunctor("level_button5",UI_DELEGATE(FIGUILevelSelect, OnButtonLevel_5));
	SetWidgetFunctor("level_button6",UI_DELEGATE(FIGUILevelSelect, OnButtonLevel_6));
	SetWidgetFunctor("level_button7",UI_DELEGATE(FIGUILevelSelect, OnButtonLevel_7));

	// disable it for now
	if(GetWidget("level_button8"))
		GetWidget("level_button8")->SetIsVisible(false);


	SetWidgetFunctor("level_button8",UI_DELEGATE(FIGUILevelSelect, OnButtonLevel_8));

	//SetWidgetFunctor("BUY_FULLVERSION",UI_DELEGATE(FIGUILevelSelect, OnButtonFullVersion));

	
	
	SetWidgetFunctor("start_button",UI_DELEGATE(FIGUILevelSelect, OnButtonPlay));	
	SetupTransition("hidden_playbutton", "comic_menu");
	   
	ui::UIWidget* pTempWidget = GetWidget("buy_box");
	if(pTempWidget)
		pTempWidget->SetIsVisible(false);
	pTempWidget = GetWidget("frame");
	if(pTempWidget)
		pTempWidget->SetIsVisible(false);
	pTempWidget = GetWidget("buybox_buttons");
	if(pTempWidget)
		pTempWidget->SetIsVisible(false);


}

bool		FIGUILevelSelect::OnEnter()
{
	// testing
	//((FIGVars*)FIGVars::Instance())->SetMaxPlayableLevel(7);

	// deprecated DIFFICULTY
	// all are now Normal
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_DIFFICULTY,1);


	// reset highscore
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_HIGHSCORE,0);

	int HardModeEnable = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_IS_HARD_PLAYABLE);
	
	EnableDisableCheckBox(HardModeEnable > 0 , "hard_button", "hard_text");
	
	// Reload Difficulty from last setting
	if(m_pUIEasy)
		m_pUIEasy->SetChecked(false);
	if(m_pUINormal)
		m_pUINormal->SetChecked(false);
	if(m_pUIHard)
		m_pUIHard->SetChecked(false);    

	int DIfficulty = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_DIFFICULTY);
	switch(DIfficulty)
	{
	case 0:
		if(m_pUIEasy)
			m_pUIEasy->SetChecked(true);
		break;
	case 1:
		if(m_pUINormal)
			m_pUINormal->SetChecked(true);
		break;
	case 2:
		if(m_pUIHard)
			m_pUIHard->SetChecked(true);    
		break;
	};

	RefreshByDifficulty();
	return FIGMOGAUIState::OnEnter();

}			  




bool		FIGUILevelSelect::Tick(float dt)
{

	RefreshUI();
    
    return    gameflow::UIState::Tick(dt);
}


void DELEGATE_CALL	FIGUILevelSelect::OnButtonEasy(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_DIFFICULTY,0);
	FIGVars::Instance()->Save();

	if(m_pUIEasy)
		m_pUIEasy->SetChecked(true);

	if(m_pUINormal)
		m_pUINormal->SetChecked(false);

	if(m_pUIHard)
		m_pUIHard->SetChecked(false);    

	RefreshByDifficulty();

}

void DELEGATE_CALL	FIGUILevelSelect::OnButtonNormal(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_DIFFICULTY,1);
	FIGVars::Instance()->Save();

	if(m_pUIEasy)
		m_pUIEasy->SetChecked(false);  

	if(m_pUINormal)
		m_pUINormal->SetChecked(true);

	if(m_pUIHard)
		m_pUIHard->SetChecked(false);

	RefreshByDifficulty();
}

void DELEGATE_CALL	FIGUILevelSelect::OnButtonHard(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_DIFFICULTY,2);
	FIGVars::Instance()->Save();

	if(m_pUIEasy)
		m_pUIEasy->SetChecked(false);

	if(m_pUINormal)
		m_pUINormal->SetChecked(false);

	if(m_pUIHard)
		m_pUIHard->SetChecked(true);

	RefreshByDifficulty();
   
}

void DELEGATE_CALL		FIGUILevelSelect::OnButtonFullVersion(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	//FIGUIRemoveAd::Instance().Show(NULL);


}



void DELEGATE_CALL	FIGUILevelSelect::OnButtonLevel_1(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	SelectLevel(1);	
}

void DELEGATE_CALL	FIGUILevelSelect::OnButtonLevel_2(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	SelectLevel(2);	
}

void DELEGATE_CALL	FIGUILevelSelect::OnButtonLevel_3(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	SelectLevel(3);	
}

void DELEGATE_CALL	FIGUILevelSelect::OnButtonLevel_4(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	SelectLevel(4);	
}

void DELEGATE_CALL	FIGUILevelSelect::OnButtonLevel_5(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	SelectLevel(5);	
}

void DELEGATE_CALL	FIGUILevelSelect::OnButtonLevel_6(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	SelectLevel(6);	
}

void DELEGATE_CALL	FIGUILevelSelect::OnButtonLevel_7(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	SelectLevel(7);	
}

void DELEGATE_CALL	FIGUILevelSelect::OnButtonLevel_8(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	SelectLevel(8);	
}

void	FIGUILevelSelect::SelectLevel(int LevelNumber)
{
	
	char	Buffer[256];

	for(int i = 0 ; i < FIGConstants::LEVEL_LAST; i++)
	{
		sprintf(Buffer, "level_button%i", i+1);
        if(GetWidget(Buffer))
            ((ui::UICheckBox*)(GetWidget(Buffer)))->SetChecked(false);
	}
	
	sprintf(Buffer, "level_button%i", LevelNumber);
    if(GetWidget(Buffer))
        ((ui::UICheckBox*)(GetWidget(Buffer)))->SetChecked(true);
	
	((FIGVars*)FIGVars::Instance())->SetCurrentLevel(LevelNumber);

	RefreshUI();
}

void		FIGUILevelSelect::EnableDisableCheckBox(bool Value,const std::string& pCheckBoxName,const std::string& pTextWidgetName)
{
	ui::UIWidget* pWidget = GetWidget(pCheckBoxName);
	
	if(pWidget)
	{
		pWidget->SetIsActive(Value);
		if(Value)
			pWidget->SetColor(1.0f,1.0f,1.0f,1.0f);	   
		else
			pWidget->SetColor(0.5f,0.5f,0.5f,0.5f);		
	
	}

	ui::UIWidget* pWidget2 = GetWidget(pTextWidgetName);

	if(pWidget2)
	{
		 pWidget2->SetIsActive(Value);
		if(Value)
			pWidget2->SetTextColor(1.0f,1.0f,1.0f,1.0f);
		
		else
			pWidget2->SetTextColor(0.5f,0.5f,0.5f,0.5f);		
	}

}

void	FIGUILevelSelect::RefreshByDifficulty()
{
	int MaxLevelToEnable = ((FIGVars*)FIGVars::Instance())->GetMaxPlayableLevel();
	if(MaxLevelToEnable == 0)
	{
		((FIGVars*)FIGVars::Instance())->FIGReset();
	}
	MaxLevelToEnable = ((FIGVars*)FIGVars::Instance())->GetMaxPlayableLevel();
	if(MaxLevelToEnable == 0)
		MaxLevelToEnable = 7;


	char	Buffer[256];
	char	Buffer2[256];

	for(int i = 1; i <= MaxLevelToEnable; i++)
	{
		sprintf(Buffer, "level_button%i", i);
		sprintf(Buffer2, "level_text%i", i);

		EnableDisableCheckBox(true,Buffer,Buffer2);
	}

	for(int i = MaxLevelToEnable + 1; i <= FIGConstants::LEVEL_LAST; i++)
	{
		sprintf(Buffer, "level_button%i", i);
		sprintf(Buffer2, "level_text%i", i);

		EnableDisableCheckBox(false,Buffer,Buffer2);
	}

	// select last level chosen
	int LastLevelChosen = ((FIGVars*)FIGVars::Instance())->GetCurrentLevel();
	SelectLevel(LastLevelChosen);
	//----------------------------------------
}

void	FIGUILevelSelect::RefreshUI()
{

	

	math::Vec2 MidPos = m_pUIPlayButton->GetPosition();
	MidPos.x = 0.5f;
	MidPos.x -= (m_pUIPlayButton->GetSize().x * 0.5f);

	//if(FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_IS_FULL_VERSION))
	{
		// FULL VERSION 
		m_pUIBuyFullVersion->SetIsVisible(false);
		m_pUIPlayButton->SetIsVisible(true);
		
		m_pUIPlayButton->SetPosition(MidPos);
	}
    /*
	else
	{
		// LITE VERSION
		m_pUIBuyFullVersion->SetIsVisible(true);
		int LevelNum = ((FIGVars*)FIGVars::Instance())->GetCurrentLevel();
		if( LevelNum < FIGConstants::PAY_VERSION_START)
		{
			m_pUIPlayButton->SetIsVisible(true);
			m_pUIBuyFullVersion->SetPosition(m_FirstButtonPos);
			m_pUIPlayButton->SetPosition(m_SecondButtonPos);
		}
		else
		{
			m_pUIPlayButton->SetIsVisible(false);
			m_pUIBuyFullVersion->SetPosition(MidPos);
		}   

	}
	*/
}


void DELEGATE_CALL		FIGUILevelSelect::OnButtonPlay(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	if (gameflow::GameFlowManager::Instance().InTransition()) 
		return;

	GetWidget("hidden_playbutton")->OnMouseRelease(GetWidget("hidden_playbutton")->GetPosition(),(int)input::Mouse::BUTTON_LEFT);
	
	char	Buffer[256];
	
	for(int i = 1; i <= FIGConstants::LEVEL_LAST; i++)
	{
		sprintf(Buffer, "level_button%i", i);
        if(GetWidget(Buffer))
            GetWidget(Buffer)->SetIsActive(false);
	}
	

}

void		FIGUILevelSelect::KeyPressed(input::Keyboard::KEY* pKey)
{
	_LOG(MSG_COMMENT,"Key pressed");
	if(pKey->KeyID == KEY_ESCAPE)
	{
		gameflow::GameFlowManager::Instance().SetTransitionEffect("fade","MainMenu","",NULL);
	}
}

bool	FIGUILevelSelect::OnUIKeyDownEvent(int	KeyCode)
{
	//_LOG(MSG_COMMENT,"test");
	switch( (input::MOGAJoystick::KEYCODE)KeyCode)
	{
		case input::MOGAJoystick::KEYCODE_BUTTON_B:				
			gameflow::GameFlowManager::Instance().SetTransitionEffect("fade","MainMenu","",NULL);
		break;
		case input::MOGAJoystick::KEYCODE_BUTTON_A:				
			OnButtonPlay(ui::UIWidget::WE_LMB_CLICK, NULL);
		break;
		case input::MOGAJoystick::KEYCODE_BUTTON_START:				
			OnButtonPlay(ui::UIWidget::WE_LMB_CLICK, NULL);
		break;

		case input::MOGAJoystick::KEYCODE_DPAD_DOWN:
			HandleMOGADpad(KeyCode);
			break;

		case input::MOGAJoystick::KEYCODE_DPAD_UP:
			HandleMOGADpad(KeyCode);
			break;

		case input::MOGAJoystick::KEYCODE_DPAD_LEFT:
			HandleMOGADpad(KeyCode);
			break;

		case input::MOGAJoystick::KEYCODE_DPAD_RIGHT:
			HandleMOGADpad(KeyCode);
			break;

		default:
			FIGMOGAUIState::OnMOGAKeyDownEvent(KeyCode);
		break;

	}
	return false;
}


void	FIGUILevelSelect::HandleMOGADpad(int KeyCode)
{
	if((int) m_UIs.size() == 0)
		return;

	// find out selection first
	ui::UIWidget* pWidget =  NULL;
	for(int i = 0; i < (int)m_UIs.size(); i++)
	{
		pWidget = m_UIs[i];
		if(pWidget && pWidget->GetType() == ui::UIWidget::WT_CHECKBOX )
		{
			if(((ui::UICheckBox*)pWidget)->IsChecked())
			{
				m_CurrentIndex = i;
				m_pCurrentSelectUI = pWidget;
				i = (int)m_UIs.size();
			}
		}
	}

	if(KeyCode == input::MOGAJoystick::KEYCODE_DPAD_RIGHT)
	{
		m_CurrentIndex++;
		if(m_CurrentIndex >= (int)m_UIs.size())
		{
			m_CurrentIndex = 0;
		}
	}
	if(KeyCode == input::MOGAJoystick::KEYCODE_DPAD_DOWN)
	{
		m_CurrentIndex+=2;
		if(m_CurrentIndex >= (int)m_UIs.size())
		{
			m_CurrentIndex = 0;
		}
	}
	else if(KeyCode == input::MOGAJoystick::KEYCODE_DPAD_LEFT)
	{
		m_CurrentIndex--;
		if(m_CurrentIndex < 0)
		{
			m_CurrentIndex = (int)m_UIs.size() - 1;
		}
	}
	else if(KeyCode == input::MOGAJoystick::KEYCODE_DPAD_UP)
	{
		m_CurrentIndex-=2;
		if(m_CurrentIndex < 0)
		{
			m_CurrentIndex = (int)m_UIs.size() - 1;
		}
	}
	
	
	m_pCurrentSelectUI = m_UIs[m_CurrentIndex];

	if(m_pCurrentSelectUI)
	{
		m_pCurrentSelectUI->OnMouseRelease(m_pCurrentSelectUI->GetPosition(), (int)input::Mouse::BUTTON_LEFT);		
	}
}



